package client_service_controller;
import java.awt.SystemTray;
import java.awt.TrayIcon;

import client_listen_update_files.ConfigManager;

import com.cloudgarden.resource.SWTResourceManager;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

public class UserNPass extends org.eclipse.swt.widgets.Composite {

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}
	
	private Label userName;
	private Text userNameText;
	private Label wrongDetails;
	private Label loginInfoLabel;
	private Button loginButton;
	private Button rememberMeButton;
	private Text passwordText;
	private Label passwordLabel;



	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI(final TrayIcon trayIcon) {
		Display display = Display.getDefault();
		final Shell shell = new Shell(display);
		UserNPass inst = new UserNPass(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		//--center console at screen--//			
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;	
		shell.setLocation(x,y);
		shell.addShellListener(new ShellAdapter() {
            public void shellClosed(ShellEvent e) {
            	SystemTray.getSystemTray().remove(trayIcon);
            	e.doit = true;
                
                }
            });
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
	}
	
	/**
	 * verify user && password before start the service
	 * @param parent
	 * @param style
	 */
	public UserNPass(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			this.setSize(357, 178);			
			{
				wrongDetails = new Label(this, SWT.NONE);
				FormData wrongDetailsLData = new FormData();
				wrongDetailsLData.left =  new FormAttachment(0, 1000, 71);
				wrongDetailsLData.top =  new FormAttachment(0, 1000, 35);
				wrongDetailsLData.width = 225;
				wrongDetailsLData.height = 20;
				wrongDetails.setLayoutData(wrongDetailsLData);
				wrongDetails.setText("Wrong user name or password!");
				wrongDetails.setFont(SWTResourceManager.getFont("Segoe UI", 9, 1, false, false));
				wrongDetails.setForeground(SWTResourceManager.getColor(255, 0, 0));
				wrongDetails.setVisible(false);
			}
			{
				loginInfoLabel = new Label(this, SWT.NONE);
				FormData loginInfoLabelLData = new FormData();
				loginInfoLabelLData.left =  new FormAttachment(0, 1000, 68);
				loginInfoLabelLData.top =  new FormAttachment(0, 1000, 3);
				loginInfoLabelLData.width = 248;
				loginInfoLabelLData.height = 33;
				loginInfoLabel.setLayoutData(loginInfoLabelLData);
				loginInfoLabel.setText("Login Information");
				loginInfoLabel.setFont(SWTResourceManager.getFont("Snap ITC", 14, 0, false, false));
			}
			{
				loginButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData loginButtonLData = new FormData();
				loginButtonLData.left =  new FormAttachment(0, 1000, 296);
				loginButtonLData.top =  new FormAttachment(0, 1000, 136);
				loginButtonLData.width = 49;
				loginButtonLData.height = 30;
				loginButton.setLayoutData(loginButtonLData);
				loginButton.setText("Login");
				loginButton.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						loginButtonWidgetSelected(evt);
					}					
				});
			}
			{
				rememberMeButton = new Button(this, SWT.CHECK | SWT.LEFT);
				FormData rememberMeButtonLData = new FormData();
				rememberMeButtonLData.left =  new FormAttachment(0, 1000, 128);
				rememberMeButtonLData.top =  new FormAttachment(0, 1000, 141);
				rememberMeButtonLData.width = 118;
				rememberMeButtonLData.height = 20;
				rememberMeButton.setLayoutData(rememberMeButtonLData);
				rememberMeButton.setText("Remember Me");
			}
			{
				FormData passwordTextLData = new FormData();
				passwordTextLData.left =  new FormAttachment(0, 1000, 128);
				passwordTextLData.top =  new FormAttachment(0, 1000, 98);
				passwordTextLData.width = 209;
				passwordTextLData.height = 20;
				passwordText = new Text(this, SWT.SINGLE | SWT.WRAP);
				passwordText.setLayoutData(passwordTextLData);
				passwordText.addSelectionListener(new SelectionAdapter() {
					public void widgetDefaultSelected(SelectionEvent evt) {
						passwordTextWidgetDefaultSelected(evt);
					}
				});
				
			}
			{
				passwordLabel = new Label(this, SWT.NONE);
				FormData passwordLabelLData = new FormData();
				passwordLabelLData.left =  new FormAttachment(0, 1000, 38);
				passwordLabelLData.top =  new FormAttachment(0, 1000, 98);
				passwordLabelLData.width = 65;
				passwordLabelLData.height = 20;
				passwordLabel.setLayoutData(passwordLabelLData);
				passwordLabel.setText("Password:");
			}
			{
				FormData userNameTextLData = new FormData();
				userNameTextLData.left =  new FormAttachment(0, 1000, 126);
				userNameTextLData.top =  new FormAttachment(0, 1000, 62);
				userNameTextLData.width = 205;
				userNameTextLData.height = 18;
				userNameText = new Text(this, SWT.SINGLE | SWT.WRAP | SWT.BORDER);
				userNameText.setLayoutData(userNameTextLData);
				userNameText.addSelectionListener(new SelectionAdapter() {					
					public void widgetDefaultSelected(SelectionEvent evt) {
						userNameTextWidgetDefaultSelected(evt);
					}
				});
				
			}
			{
				userName = new Label(this, SWT.NONE);
				FormData userNameLData = new FormData();
				userNameLData.left =  new FormAttachment(0, 1000, 38);
				userNameLData.top =  new FormAttachment(0, 1000, 62);
				userNameLData.width = 76;
				userNameLData.height = 20;
				userName.setLayoutData(userNameLData);
				userName.setText("User Name:");
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private void loginButtonWidgetSelected(SelectionEvent evt) {
		
		String userName=userNameText.getText();
		String password=passwordText.getText();
		//check if login was successful
		if (false){
		//try{			
			if (rememberMeButton.getSelection()){
				ConfigManager.get().updateConfig(userName,password);
			}
			else{
				ConfigManager.get().setTempName(userName);
				ConfigManager.get().setTempPass(password);
			}
			this.getShell().dispose();
			
		}else{		
			wrongDetails.setVisible(true);		
		}
	}
		
		private void userNameTextWidgetDefaultSelected(SelectionEvent evt) {
			loginButton.notifyListeners(SWT.Selection,new Event());	
		}
		
		private void passwordTextWidgetDefaultSelected(SelectionEvent evt) {
			loginButton.notifyListeners(SWT.Selection,new Event());	
		}
		
}
